<template>
    <div>
        <!-- Form -->
        <el-button @click="dialogFormVisible = true">添加角色</el-button>
        <el-dialog title="角色信息" :visible.sync="dialogFormVisible" @close="closeDialog">
            <el-form :model="role">
                <el-form-item label="名称" :label-width="formLabelWidth">
                    <el-input v-model="role.name" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="描述" :label-width="formLabelWidth">
                    <el-input v-model="role.description" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="站点ID" :label-width="formLabelWidth">
                    <el-input v-model="role.siteId" autocomplete="off"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveRole()">确 定</el-button>
            </div>
        </el-dialog>
        <!--权限树对话框-->
        <el-dialog title="权限信息" :visible.sync="dialogTreeVisible">
            <el-tree
                    :data="allPermissions"
                    show-checkbox
                    default-expand-all
                    node-key="id"
                    ref="tree"
                    highlight-current
                    :props="defaultProps">
            </el-tree>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogTreeVisible = false">取 消</el-button>
                <el-button type="primary" @click="setRolePermissions()">确 定</el-button>
            </div>
        </el-dialog>
        <el-table
                :data="roles"
                style="width: 100%">
            <el-table-column
                    label="编号"
                    width="180">
                <template slot-scope="scope">
                    <span style="margin-left: 10px">{{ scope.row.id }}</span>
                </template>
            </el-table-column>
            <el-table-column
                    label="名称"
                    width="180">
                <template slot-scope="scope">
                    <span style="margin-left: 10px">{{ scope.row.name }}</span>
                </template>
            </el-table-column>
            <el-table-column
                    label="描述"
                    width="180">
                <template slot-scope="scope">
                    <span style="margin-left: 10px">{{ scope.row.description }}</span>
                </template>
            </el-table-column>
            <el-table-column
                    label="站点ID"
                    width="180">
                <template slot-scope="scope">
                    <span style="margin-left: 10px">{{ scope.row.siteId }}</span>
                </template>
            </el-table-column>
            <el-table-column label="操作">
                <template slot-scope="scope">
                    <el-button
                            size="mini"
                            @click="showRolePermissions(scope.row.id)">设置权限</el-button>
                    <el-button
                            size="mini"
                            @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-popconfirm
                            confirm-button-text='好的'
                            cancel-button-text='取消'
                            icon="el-icon-info"
                            icon-color="red"
                            @confirm="handleDelete(scope.$index, scope.row)"
                            title="确定删除吗？">
                        <el-button size="mini" slot="reference">删除</el-button>
                    </el-popconfirm>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
                background
                layout="prev, pager, next"
                :page-size="pageSize"
                @current-change="loadRolePage"
                :total="total">
        </el-pagination>
    </div>
</template>

<script>
    export default {
        name: "RoleView",
        data(){
            return{
                roles:[], //角色集合
                role:{} , //当前角色
                current: 1,     //当前页数
                pageSize: 10,    //分页长度
                total: 0,        // 数据总个数
                dialogFormVisible: false, //控制对话框的显示
                formLabelWidth: '120px',
                dialogTreeVisible: false, //树对话框的显示
                allPermissions:[], //所有权限
                rolePermissions:[], //角色的权限
                defaultProps: {
                    children: 'subPermissions',
                    label: 'name'
                },
                roleId: -1  //当前的角色
            }
        },
        methods:{
            //分页查询
            loadRolePage(current){
                this.current = current;
                //调用后台分页查询接口
                this.axios.get("http://localhost:8080/roles/page?current=" + this.current +"&pageSize=" + this.pageSize)
                    .then(result => {
                        if(result.data.status == "OK"){
                            //获得数据集合
                            this.roles = result.data.data.records;
                            this.total = result.data.data.total;
                        }
                    });
            },
            //完成回显
            handleEdit(index, row) {
                console.log(index, row);
                this.role = JSON.parse(JSON.stringify(row));
                this.dialogFormVisible = true;
            },
            //删除
            handleDelete(index, row) {
                console.log(index, row);
                //后台删除角色
                this.axios.delete("http://localhost:8080/role/" + row.id)
                    .then(result => {
                        if(result.data.status == "OK"){
                            this.loadRolePage(this.current);
                        }
                    })
            },
            //添加或更新角色
            saveRole(){
                //通过角色id判断是否进行添加或更新
                if(this.role.id){
                    //存在id，就更新
                    this.axios.put("http://localhost:8080/role",
                        this.role)
                        .then(result => {
                            if(result.data.status == "OK"){
                                this.loadRolePage(this.current);
                            }
                        });
                }else{
                    //不存在id，就添加
                    this.axios.post("http://localhost:8080/role",
                        this.role)
                        .then(result => {
                            if(result.data.status == "OK"){
                                this.loadRolePage(this.current);
                            }
                        });
                }
                this.dialogFormVisible = false;
            },
            //对话框关闭
            closeDialog(){
                this.role = {};
            },
            //显示角色的权限
            showRolePermissions(roleId){
                this.roleId = roleId;
                this.dialogTreeVisible = true;
                //先查询所有权限
                this.axios.get("http://localhost:8080/permissions")
                    .then(result => {
                       if(result.data.status == "OK"){
                           this.allPermissions = result.data.data;
                           //再查询用户的权限
                           this.axios.get("http://localhost:8080/permissions/role?roleId=" + roleId)
                               .then(result => {
                                   if(result.data.status == "OK"){
                                       this.rolePermissions = result.data.data;
                                       // 设置勾选效果
                                       this.$refs.tree.setCheckedNodes(this.rolePermissions);
                                   }
                               });
                       }
                    });
            },
            // 设置权限
            setRolePermissions(){
                //获得所有选中节点的key
                let keys = this.$refs.tree.getCheckedKeys();
                let keyStr = "";
                for(let i = 0;i < keys.length;i++){
                    keyStr += keys[i] + ",";
                }
                keyStr = keyStr.substr(0, keyStr.length - 1);
                //调用后台接口
                this.axios.post("http://localhost:8080/permissions/role-set",
                    "roleId="+ this.roleId +"&permissionIds=" + keyStr)
                    .then(result => {
                        if(result.data.status == "OK"){
                            this.$message(result.data.data);
                        }
                    });
                this.dialogTreeVisible = false;
            }
        },
        mounted(){  //生命周期函数 界面挂载完成
            this.loadRolePage(1);
        }
    }
</script>

<style scoped>

</style>